# require 'appengine-apis/users'

## 使わないかも
def login_require(redirect_to_url = '/')
  @user = User.first
  if @user.nil?
    @user = User.create({
        :name => 'maimuzo',
        :email => 'maimuzo@gmail.com',
        :created_at => Time.now
      })
  end

  @logger.debug ' @user : ' + @user.inspect
end

# 人間様用
def google_login_require(redirect_to_url = '/')
  login_require(redirect_to_url)
end

# Android様用
def basic_auth_require(error_message = 'Check your Google account address and the password for this service!')
  @logger.debug "enter basic_auth"

  @auth ||=  Rack::Auth::Basic::Request.new(request.env)
  if @auth.provided? and @auth.basic? and @auth.credentials
    username = @auth.credentials[0]
    password = @auth.credentials[1]
    @logger.debug "input username: #{username} / password: #{password}"
    @user = User.first(:email => username, :password => password)
  end

# 何故か使えない…
#  use Rack::Auth::Basic do |username, password|
#    @logger.debug "input username: #{username} / password: #{password}"
#    @user = User.first(:email => username, :password => password)
#  end
  @logger.debug "pass basic_auth"

  if @user.nil?
    response.header["WWW-Authenticate"] = 'Basic realm="' + error_message + '"'
    halt 401, error_message
  end
end

def setup_logger
  @logger = Logger.new(STDOUT)
end

def google_logout(path)
  path
end
